package com.neu.carbon.mes.controller;

import java.util.Date;
import java.util.List;

import com.neu.carbon.mes.domain.MesCheckStandard;
import com.neu.carbon.mes.domain.MesProductJob;
import com.neu.carbon.mes.domain.MesProductPlan;
import com.neu.carbon.mes.service.IMesCheckStandardService;
import com.neu.carbon.mes.service.IMesProductJobService;
import com.neu.carbon.mes.service.IMesProductPlanService;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.service.IWmsMaterialInfoService;
import com.neu.common.enums.SerialNoPrefix;
import com.neu.smty.utils.CodeUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.neu.common.annotation.Log;
import com.neu.common.core.controller.BaseController;
import com.neu.common.core.domain.AjaxResult;
import com.neu.common.enums.BusinessType;
import com.neu.carbon.mes.domain.MesProcessCheck;
import com.neu.carbon.mes.service.IMesProcessCheckService;
import com.neu.common.utils.poi.ExcelUtil;
import com.neu.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 生产采样单Controller
 * 
 * @author neuedu
 * @date 2024-09-11
 */
@Api(tags = {"生产采样单"})
@RestController
@RequestMapping("/mesCheck/processCheck")
public class MesProcessCheckController extends BaseController
{
    @Autowired
    private IMesProcessCheckService mesProcessCheckService;
    @Resource
    private IWmsMaterialInfoService iWmsMaterialInfoService;
    @Resource
    private IMesProductJobService iMesProductJobService;
    @Resource
    private IMesProductPlanService iMesProductPlanService;
    @Resource
    private IMesCheckStandardService iMesCheckStandardService;


    /**
     * 查询生产采样单列表
     */
    @GetMapping("/list")
    @ApiOperation("查询生产采样单列表")
    @DynamicResponseParameters(properties = {
	        @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = MesProcessCheck.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public TableDataInfo list(MesProcessCheck mesProcessCheck)
    {
        startPage();
        List<MesProcessCheck> list = mesProcessCheckService.selectMesProcessCheckList(mesProcessCheck);


        list.forEach(model->{
            if(model.getCheckUser().equals("1")){
                model.setCheckUserName("管理员");
            }
            MesCheckStandard mesCheckStandard = iMesCheckStandardService.selectMesCheckStandardById(model.getCheckStandardId());
            if (mesCheckStandard!=null){
                model.setCheckStandardName(mesCheckStandard.getCheckItemName());
            }
            WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getMaterialId());
            if (wmsMaterialInfo!=null){
                model.setMaterialName(wmsMaterialInfo.getName());
                model.setModel(wmsMaterialInfo.getModel());
                model.setSpecification(wmsMaterialInfo.getSpecification());
                model.setUnit(wmsMaterialInfo.getUnit());
            }
            MesProductJob mesProductJob = iMesProductJobService.selectMesProductJobById(model.getProductJobId());
            if (mesProductJob!=null){
                model.setProductJobName(mesProductJob.getName());
            }
            MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getProductPlanId());
            if (iMesProductPlanService!=null){
                model.setProductPlanName(mesProductPlan.getSerialNo());
            }

        });
        return getDataTable(list);
    }

    /**
     * 导出生产采样单列表
     */
    @ApiOperation("导出生产采样单列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "文件名")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:processCheck:export')")
    @Log(title = "生产采样单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MesProcessCheck mesProcessCheck)
    {
        List<MesProcessCheck> list = mesProcessCheckService.selectMesProcessCheckList(mesProcessCheck);
        ExcelUtil<MesProcessCheck> util = new ExcelUtil<MesProcessCheck>(MesProcessCheck.class);
        return util.exportExcel(list, "processCheck");
    }

    /**
     * 获取生产采样单详细信息
     */
    @ApiOperation("获取生产采样单详细信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "data", value = "返回业务数据", dataTypeClass = MesProcessCheck.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        MesProcessCheck model = mesProcessCheckService.selectMesProcessCheckById(id);
        if(model.getCheckUser().equals("1")){
            model.setCheckUserName("管理员");
        }
        WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getMaterialId());
        if (wmsMaterialInfo!=null){
            model.setMaterialName(wmsMaterialInfo.getName());
            model.setModel(wmsMaterialInfo.getModel());
            model.setSpecification(wmsMaterialInfo.getSpecification());
            model.setUnit(wmsMaterialInfo.getUnit());
        }
        MesProductJob mesProductJob = iMesProductJobService.selectMesProductJobById(model.getProductJobId());
        if (mesProductJob!=null){
            model.setProductJobName(mesProductJob.getName());
        }
        MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getProductPlanId());
        if (mesProductPlan!=null){
            model.setProductPlanName(mesProductPlan.getSerialNo());
        }
        MesCheckStandard mesCheckStandard = iMesCheckStandardService.selectMesCheckStandardById(model.getCheckStandardId());
        if (mesCheckStandard!=null){
            model.setCheckStandardName(mesCheckStandard.getCheckItemName());
        }

        return AjaxResult.success(model);
    }

    /**
     * 新增生产采样单
     */
    @ApiOperation("新增生产采样单")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:processCheck:add')")
    @Log(title = "生产采样单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProcessCheck mesProcessCheck)
    {
        mesProcessCheck.setSerialNo(CodeUtil.getSerialNo(SerialNoPrefix.CPJY));
        mesProcessCheck.setCheckDate(new Date());
        return toAjax(mesProcessCheckService.insertMesProcessCheck(mesProcessCheck));
    }

    /**
     * 质量追溯
     * 修改生产采样单
     */
    @ApiOperation("修改生产采样单")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:processCheck:edit')")
    @Log(title = "生产采样单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProcessCheck mesProcessCheck)
    {
        return toAjax(mesProcessCheckService.updateMesProcessCheck(mesProcessCheck));
    }

    /**
     * 删除生产采样单
     */
    @ApiOperation("删除生产采样单")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:processCheck:remove')")
    @Log(title = "生产采样单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesProcessCheckService.deleteMesProcessCheckByIds(ids));
    }
}
